class AccountController < ApplicationController
  before_filter :login_required, :only => [ :index, :select_character, :choose_character ]
  
  def select_character
    @clear = params[:clear]
    @realm = params[:realm] unless @clear
    @territory = Kingdom.realm_choice @realm
    @title_choice = params[:title] unless @clear
    title_array = Title.title_choice @title_choice
    @sex = params[:sex] unless "" == params[:sex] or @clear
    @characters = Character.find_all_npcs_by_realm_and_title_and_sex(@territory, title_array[0], title_array[1], @sex)
    
  end
  
  def choose_character
    if params[:character_id].nil?
      redirect_to(:action => 'select_character')
      return
    end
    @character = Character.find(params[:character_id])
    raise "Can only take over NPCs!" unless @character.npc?
    current_pc = current_player.player_character
    if current_pc
      current_pc.player_id = 0
      current_pc.save
    end
    @character.player_id = self.current_player.id
    @character.save
    redirect_to(:action => 'index')
    
  rescue
    flash[:notice] = "Invalid character selected"
    redirect_to(:action => 'select_character')
  end
  
  def index
    if self.current_player.admin?
      redirect_to(:action => 'admin_home')
    end
  end

  def forgot_password
    return unless request.post?
    if @player = Player.find_by_email(params[:email])
      @player.forgot_password
      @player.save
      redirect_back_or_default(:controller => '/account', :action => 'index')
      flash[:notice] = "A password reset link has been sent to your email address" 
    else
      flash[:notice] = "Could not find a user with that email address" 
    end
  end

  def reset_password
    @player = Player.find_by_password_reset_code(params[:id])
    raise if @player.nil?
    return if @player unless params[:password]
      if (params[:password] == params[:password_confirmation])
        self.current_player = @player #for the next two lines to work
        current_player.password_confirmation = params[:password_confirmation]
        current_player.password = params[:password]
        @player.reset_password
        flash[:notice] = current_player.save ? "Password reset" : "Password not reset" 
      else
        flash[:notice] = "Password mismatch" 
      end  
      redirect_back_or_default(:controller => '/account', :action => 'index') 
  rescue
    logger.error "Invalid Reset Code entered" 
    flash[:notice] = "Sorry - That is an invalid password reset code. Please check your code and try again. (Perhaps your email client inserted a carriage return?" 
    redirect_back_or_default(:controller => '/account', :action => 'index')
  end
  
  def activate
    flash.clear  
    return if params[:id]  == nil and params[:activation_code]  == nil
    activator = params[:id] || params[:activation_code]
    @player = Player.find_by_activation_code(activator) 
    if @player and @player.activate
      redirect_back_or_default(:controller => '/account', :action => 'login')
      flash[:notice] = "Your account has been activated.  Please login."
    else
      flash[:notice] = "Unable to activate the account.  Please check or enter manually."
    end
  end

  def login
    return unless request.post?
    self.current_player = Player.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        self.current_player.remember_me
        cookies[:auth_token] = { :value => self.current_player.remember_token , :expires => self.current_player.remember_token_expires_at }
      end
      redirect_back_or_default(:controller => '/account', :action => 'index')
      flash[:notice] = "Logged in successfully"
    end
  end

  def signup
    @player = Player.new(params[:player])
    return unless request.post?
    @player.save!
    if @player.id == 1
      # first registered player is the game admin
      GameConfig.set_admin_player @player
      self.current_player = @player
      @player.activate
      render :action => 'admin_home'
    else
      render :action => 'welcome'
    end
    
  rescue ActiveRecord::RecordInvalid
    render :action => 'signup'
  end
  
  def logout
    self.current_player.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "You have been logged out."
    redirect_back_or_default(:controller => '/account', :action => 'index')
  end
end
